﻿// ObjectManager.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Casters.source.Objects;
#endregion

namespace Casters.source.Managers
{
    static public class CObjectManager
    {
        public static List<CBaseObject> m_ListOfObjects = null;

        static public int ListSize { get { return m_ListOfObjects.Count; } }

        /// <summary>
        /// Prepares the Object Manager for a fresh start
        /// Creates the list if there is none
        /// Clears out the list if there is already stuff in it
        /// </summary>
        static public void Initialize()
        {
            if (m_ListOfObjects == null)
                m_ListOfObjects = new List<CBaseObject>();

            if (ListSize > 0)
                Clear();
        }

        /// <summary>
        /// Cleans up the Object Manager
        /// </summary>
        static public void ShutDown()
        {
            Clear();
        }

        /// <summary>
        /// Updates all of the objects contained
        /// Cleans up dead objects
        /// </summary>
        /// <param name="dt">Delta Time</param>
        static public void Update(float dt)
        {
            List<CBaseObject> m_ObjectsToRemove = new List<CBaseObject>();

            foreach (CBaseObject obj in m_ListOfObjects)
            {
                if (obj.m_bIsAlive)
                    obj.Update(dt);
                else
                    m_ObjectsToRemove.Add(obj);
            }

            foreach (CBaseObject obj in m_ObjectsToRemove)
                RemoveObject(obj);

            m_ObjectsToRemove.Clear();
        }

        /// <summary>
        /// Draws all of the objects contained
        /// </summary>
        static public void Draw()
        {
            foreach (CBaseObject obj in m_ListOfObjects)
                if(CGame.GetInstance().m_Camera.CheckCulling(obj.m_vPosition))
                    obj.Draw();
        }

        /// <summary>
        /// Adds an object to the list
        /// </summary>
        /// <param name="obj">Object to add</param>
        static public void AddObject(CBaseObject obj)
        {
            if (obj == null)
                return;

            obj.LoadContent();
            m_ListOfObjects.Add(obj);
        }

        /// <summary>
        /// Removes an object from the list
        /// </summary>
        /// <param name="obj">Object to remove</param>
        static public void RemoveObject(CBaseObject obj)
        {
            if (obj == null)
                return;

            obj.UnloadContent();
            m_ListOfObjects.Remove(obj);
        }

        /// <summary>
        /// Clears the list
        /// </summary>
        static public void Clear()
        {
            foreach (CBaseObject obj in m_ListOfObjects)
                obj.UnloadContent();

            m_ListOfObjects.Clear();
        }
    }
}
